//
//  LYRadarView.m
//  Radar
//
//  Created by 吴浪 on 16/2/1.
//  Copyright © 2016年 吴浪. All rights reserved.
//

#import "LYRadarView.h"
#import "MYColor.h"

@implementation LYRadarView
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        _targets = 1;
        _targetColor = [UIColor redColor];
        _pulsings = 5;
        _pulsingColor = [UIColor whiteColor];
        _animationTimes = 3.0;
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    
    //防止多次绘制图层重复添加
    for (CALayer *layer in self.layer.sublayers) {
        [layer removeFromSuperlayer];
    }
    
    CGFloat x;
    CGFloat y;
    CGFloat w;
    CGFloat h;
    
    //波形
    CALayer *animaLayer = [CALayer layer];
    CAMediaTimingFunction *defaultCurve = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
    for (int i = 0; i < self.pulsings; i++) {
        CALayer *pulsingLayer = [CALayer layer];
        pulsingLayer.frame = CGRectMake(0, 0, rect.size.width, rect.size.height);
        pulsingLayer.borderColor = _pulsingColor.CGColor;
        pulsingLayer.borderWidth = 2.0;
        pulsingLayer.cornerRadius = rect.size.width * 0.5;
        
        
        CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
        animationGroup.fillMode = kCAFillModeBackwards;
        animationGroup.beginTime = CACurrentMediaTime() + (double)i * _animationTimes / _pulsings;
        animationGroup.duration = _animationTimes;
        animationGroup.repeatCount = HUGE;
        animationGroup.timingFunction = defaultCurve;
        
        CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
        scaleAnimation.fromValue = @0.5;
        scaleAnimation.toValue = @3.0;
        
        CAKeyframeAnimation *opacityAnimation = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
        opacityAnimation.values =  @[@0.0, @0.1, @0.3, @0.8, @1.0, @0.8, @0.4, @0.3, @0.2, @0.1, @0];
        opacityAnimation.keyTimes = @[@0, @0.1, @0.2, @0.3, @0.4, @0.5, @0.6, @0.7, @0.8, @0.9, @1];
        animationGroup.animations = @[scaleAnimation, opacityAnimation];
        [pulsingLayer addAnimation:animationGroup forKey:@"plulsing"];
        [animaLayer addSublayer:pulsingLayer];
    }
    
    //小点
    for (int i = 0; i < _targets; i++) {
        NSInteger dianR = rect.size.width;
        CALayer *dianLayer = [[CALayer alloc] init];
        x = pow(-1, arc4random()) * (arc4random() % dianR + 20);
        y = pow(-1, arc4random()) * (arc4random() % dianR + 20);
        w = arc4random() % 10 + 5;
        h = w;
        dianLayer.frame = CGRectMake(x, y, w, h);
        dianLayer.backgroundColor = _targetColor.CGColor;
        dianLayer.cornerRadius = w * 0.5;
        
        CAAnimationGroup *dianGroup = [CAAnimationGroup animation];
        dianGroup.beginTime = CACurrentMediaTime() + (double)i * _animationTimes;
        dianGroup.duration = _animationTimes;
        dianGroup.repeatCount = HUGE;
        dianGroup.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        CABasicAnimation *dianBasic = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
        dianBasic.fromValue = @0.5;
        dianBasic.toValue = @1.5;
        
        CAKeyframeAnimation *keyAn = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
        keyAn.values = @[@0.0, @1.0, @0.0];
        keyAn.keyTimes = @[@0.1, @0.5, @1.0];
        dianGroup.animations = @[dianBasic, keyAn];
        [dianLayer addAnimation:dianGroup forKey:@"plulsing"];
        dianLayer.opacity = 0.0;
        [animaLayer addSublayer:dianLayer];
    }
    
    //扫描线
    w = 200;
    h = w;
    x = (rect.size.width - w) * 0.5;
    y = (rect.size.height - h) * 0.5;
    CAShapeLayer *layerLine = [CAShapeLayer layer];
    layerLine.frame = CGRectMake(x, y, w, h);
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(0, 0)];
    [path addLineToPoint:CGPointMake(w * 0.5, h * 0.5)];
    [path closePath];
    path.lineWidth = 2.0;
    layerLine.path = path.CGPath;
    layerLine.strokeColor = [UIColor blueColor].CGColor;
    
    CABasicAnimation *lineBasic = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    lineBasic.fromValue = @(0);
    lineBasic.toValue = @(M_PI * 2);
    CAAnimationGroup *anGroup = [CAAnimationGroup animation];
    //    anGroup.fillMode = kCAFillModeForwards;
    //    anGroup.removedOnCompletion = NO;
    anGroup.repeatCount = HUGE;
    anGroup.beginTime = CACurrentMediaTime();
    anGroup.duration = _animationTimes;
    anGroup.animations = @[lineBasic];
    [layerLine addAnimation:anGroup forKey:nil];
    [animaLayer addSublayer:layerLine];
    
    [self.layer addSublayer:animaLayer];

    
}
@end



////
////  LYRadarView.m
////  Radar
////
////  Created by 吴浪 on 16/2/1.
////  Copyright © 2016年 吴浪. All rights reserved.
////
//
//#import "LYRadarView.h"
//#import "MYColor.h"
//
//@implementation LYRadarView
//- (void)drawRect:(CGRect)rect {
//    [super drawRect:rect];
////    [[UIColor greenColor] setFill];
//    UIRectFill(rect);
//    
//    for (CALayer *layer in self.layer.sublayers) {
//        [layer removeFromSuperlayer];
//    }
//    NSInteger pulsingCount = 5;
//    double animationDuration = 3;
//    CALayer *animaLayer = [CALayer layer];
//    for (int i = 0; i < pulsingCount; i++) {
//        CALayer *pulsingLayer = [CALayer layer];
//        pulsingLayer.frame = CGRectMake(0, 0, rect.size.width, rect.size.height);
//        pulsingLayer.borderColor = [UIColor whiteColor].CGColor;
//        pulsingLayer.borderWidth = 2.0;
//        pulsingLayer.cornerRadius = rect.size.height * 0.5;
////        pulsingLayer.backgroundColor = [UIColor greenColor].CGColor;
//        
//        CAMediaTimingFunction *defaultCurve = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
//        
//        CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
//        animationGroup.fillMode = kCAFillModeBackwards;
//        animationGroup.beginTime = CACurrentMediaTime() + (double)i * animationDuration / pulsingCount;
//        animationGroup.duration = animationDuration;
//        animationGroup.repeatCount = HUGE;
//        animationGroup.timingFunction = defaultCurve;
//        
//        CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
//        scaleAnimation.fromValue = @0.5;
//        scaleAnimation.toValue = @3.0;
//        
//        CAKeyframeAnimation *opacityAnimation = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
////        opacityAnimation.values =  @[@1, @0.9, @0.8, @0.7, @0.6, @0.5, @0.4, @0.3, @0.2, @0.1, @0];
////        opacityAnimation.keyTimes = @[@0, @0.1, @0.2, @0.3, @0.4, @0.5, @0.6, @0.7, @0.8, @0.9, @1];
//        opacityAnimation.values =  @[@0.0, @0.1, @0.3, @0.8, @1.0, @0.8, @0.4, @0.3, @0.2, @0.1, @0];
//        opacityAnimation.keyTimes = @[@0, @0.1, @0.2, @0.3, @0.4, @0.5, @0.6, @0.7, @0.8, @0.9, @1];
//        animationGroup.animations = @[scaleAnimation, opacityAnimation];
//        [pulsingLayer addAnimation:animationGroup forKey:@"plulsing"];
//        [animaLayer addSublayer:pulsingLayer];
//        
//        //小点
//        CALayer *dianLayer = [[CALayer alloc] init];
//        CGFloat x = pow(-1, arc4random()) * (arc4random() % 100 + 20);
//        CGFloat y = pow(-1, arc4random()) * (arc4random() % 100 + 20);
//        CGFloat w = arc4random() % 10 + 5;
//        CGFloat h = w;
//        dianLayer.frame = CGRectMake(x, y, w, h);
//        dianLayer.backgroundColor = [UIColor redColor].CGColor;
//        dianLayer.cornerRadius = w * 0.5;
//        
//        CAAnimationGroup *anGroup = [CAAnimationGroup animation];
////        anGroup.fillMode = kCAFillModeForwards;
////        anGroup.removedOnCompletion = NO;
//        anGroup.beginTime = CACurrentMediaTime() + (double)i * animationDuration;
//        anGroup.duration = animationDuration;
//        anGroup.repeatCount = HUGE;
//        anGroup.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
//        CABasicAnimation *basicAn = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
//        basicAn.fromValue = @0.5;
//        basicAn.toValue = @1.5;
//        
//        CAKeyframeAnimation *keyAn = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
////        keyAn.values = @[@0, @0.1, @0.8, @1.0, @0.8, @0.6, @0.4, @0.1, @0.0];
////        keyAn.keyTimes = @[@0.1, @0.3, @0.4, @0.5, @0.8, @0.9, @1.0];
//        keyAn.values = @[@0.0, @1.0, @0.0];
//        keyAn.keyTimes = @[@0.1, @0.5, @1.0];
//        anGroup.animations = @[basicAn, keyAn];
//        [dianLayer addAnimation:anGroup forKey:@"plulsing"];
//        dianLayer.opacity = 0.0;
//        [animaLayer addSublayer:dianLayer];
//    }
//    
//    CGFloat w = 200;
//    CGFloat h = w;
//    CGFloat x = (rect.size.width - w) * 0.5;
//    CGFloat y = (rect.size.height - h) * 0.5;
//    CAShapeLayer *layerLine = [CAShapeLayer layer];
//    layerLine.frame = CGRectMake(x, y, w, h);
////    layerLine.backgroundColor = [UIColor grayColor].CGColor;
//    UIBezierPath *path = [UIBezierPath bezierPath];
//    [path moveToPoint:CGPointMake(0, 0)];
//    [path addLineToPoint:CGPointMake(w * 0.5, h * 0.5)];
//    [path closePath];
//    path.lineWidth = 2.0;
//    layerLine.path = path.CGPath;
//    layerLine.strokeColor = [UIColor blueColor].CGColor;
//
//    CABasicAnimation *basicAn = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
//    basicAn.fromValue = @(0);
//    basicAn.toValue = @(M_PI * 2);
//    CAAnimationGroup *anGroup = [CAAnimationGroup animation];
////    anGroup.fillMode = kCAFillModeForwards;
////    anGroup.removedOnCompletion = NO;
//    anGroup.repeatCount = HUGE;
//    anGroup.beginTime = CACurrentMediaTime();
//    anGroup.duration = animationDuration;
//    anGroup.animations = @[basicAn];
//    [layerLine addAnimation:anGroup forKey:nil];
//    
//    [animaLayer addSublayer:layerLine];
//    [self.layer addSublayer:animaLayer];
////    [self.layer addSublayer:layerLine];
//    
//}
//@end
